package DMSXL._2024.BinaryTrees;

import java.util.LinkedList;
import java.util.Queue;

public class countNodes {
    public static void main(String[] args) {

    }
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int k = queue.size();
            for (int i = 0; i < k; i++) {
                TreeNode cur = queue.poll();
                count++;
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
        }
        return count;

    }
}
